<template>
	<div id="app">
		<router-view/>
	</div>
</template>

<script>
import axios from 'axios';
import qs from 'qs';
	export default {
    name: 'App',
    mounted () {
      if ( this.$cookie.get ( 'serviceId' )) {
				this.$store.commit ( 'userId', this.$cookie.get ( 'serviceId' ));
			}
			if ( this.$cookie.get ( 'servicePwd' )) {
				this.$store.commit ( 'servicePwd', this.$cookie.get ( 'servicePwd' ));
      }
      
    },
		created () {
			if ( this.$cookie.get ( 'serviceId' )) {
				this.$store.commit ( 'userId', this.$cookie.get ( 'serviceId' ));
			}
			if ( this.$cookie.get ( 'servicePwd' )) {
				this.$store.commit ( 'servicePwd', this.$cookie.get ( 'servicePwd' ));
      }

    },
    watch: {
      $route:{
        handler(val,oldval){
          let that = this
          if(this.$store.state.userId==-1){
            this.$router.push({
                path:'/'
            })
          }else if(this.$cookie.get('servicePwd') === 'yunlehui2019' && val != '/cpass'){
            this.$router.push({
                path:'/cpass'
            })
          }
          if(this.$router.name != 'login') {
            axios({
        method: "post",
        url: `${that.$store.state.url}/service/checkStates`,
        data: qs.stringify({
          serviceId: that.$cookie.get("serviceId")
        })
      }).then(res => {
        if (res.data.code != 0) {
          that.$alert(res.data.msg);
          that.$router.push({
            path: "/"
          });
          that.$cookie.delete("serviceId");
        }
      });
          }
        },
        // 深度观察监听
        deep: true
      }
    }
	}
</script>

<style>
	html,body{
		margin: 0;
		padding: 0;
	}
</style>
